package com.ymt.fmd.platform.web.rest;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.ymt.fmd.platform.service.PServiceService;
import com.ymt.fmd.util.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

@Component("fmdServiceResource")
@Path("/service")
public class ServiceResource {

	private Logger logger = Logger.getLogger(ServiceResource.class.getName());
	
	@Autowired
	private PServiceService ss;
	
	@Context
	private HttpServletRequest request;

	@GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
	public String get(@PathParam("id") String id, @QueryParam("timezoneOffset")int timezoneOffset) {
		logger.finer("get id="+id);
		String lang = "zh";
		return ss.get(id, lang, timezoneOffset);
	}
	
	@POST
    @Path("/save")
	@Produces(MediaType.APPLICATION_JSON)
	public String save(@FormParam("bostr") String bostr) {
		logger.finer("save bostr="+bostr);
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		String loginName = userDetail.get(Const.LOGINNAME);
		String displayName = userDetail.get(Const.DISPLAYNAME);
		String lang = userDetail.get(Const.LANG);
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("save lang="+lang);
		logger.finer("save tenantId="+tenantId);
		logger.finer("save loginName="+loginName);
		logger.finer("save displayName="+displayName);
		String id = ss.save(tenantId, bostr, loginName, displayName, lang);
		return "{\"result\":true, \"id\":\""+id+"\"}";
	}
	
	@DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
	public String delete(@PathParam("id") String id) {
		logger.finer("delete id="+id);
		int rs = ss.delete(id);
		return "{\"result\":"+rs+"}";
	}
	
	@GET
    @Path("delete/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public String del(@PathParam("id") String id) {
		logger.finer("delete id="+id);
		int rs = ss.delete(id);
		return "{\"result\":"+rs+"}";
	}
	
	@POST
    @Path("/query")
    @Produces(MediaType.APPLICATION_JSON)
	public String query(@FormParam("queryParams")String queryParams,
			@FormParam("fromRow")int fromRow,
			@FormParam("toRow")int toRow,
			@FormParam("timezoneOffset")int timezoneOffset,
			@FormParam("ordercolumns")String ordercolumns) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		logger.finer("query queryParams="+queryParams);
		String lang = userDetail.get(Const.LANG);
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("query lang="+lang);
		logger.finer("query tenantId="+tenantId);
		return ss.query(tenantId, queryParams, fromRow, toRow, timezoneOffset, lang, ordercolumns);
	}
	
	@GET
	@Path("/find")
	@Produces(MediaType.APPLICATION_JSON)
	public String find(@QueryParam("start") int start, @QueryParam("limit") int limit,@QueryParam("timezoneOffset") int timezoneOffset,
			@QueryParam("ordercolumns")String ordercolumns, @QueryParam("query") String queryParams) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		logger.finer("find queryParams="+queryParams);
		String lang = userDetail.get(Const.LANG);
		String tenantId = userDetail.get(Const.TENANT_ID);
		logger.finer("find lang="+lang);
		logger.finer("find tenantId="+tenantId);
		Map res = ss.find(tenantId, start, limit, timezoneOffset, lang, queryParams,ordercolumns);
		return JSON.toJSONString(res);
	}

	@POST
	@Path("/callRestService")
	@Produces(MediaType.APPLICATION_JSON)
	public String callRestService(@FormParam("svcId")String svcId, 
			@FormParam("intfvarsStr")String intfvarsStr) {
		JSONArray intfvars = JSONArray.parseArray(intfvarsStr);
		try {
			return ss.callRestService(svcId, intfvars);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@POST
    @Path("/queTab")
    @Produces(MediaType.APPLICATION_JSON)
	public String queryTable(@FormParam("tableName")String tableName,
			@FormParam("fromRow")int fromRow,
			@FormParam("toRow")int toRow,
			@FormParam("timezoneOffset")int timezoneOffset,
			@FormParam("ordercolumns")String ordercolumns) {
		Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
		logger.finer("query tableName="+tableName);
		String lang = userDetail.get(Const.LANG);
		logger.finer("query lang="+lang);
		return ss.queryTable(tableName, fromRow, toRow, timezoneOffset, lang, ordercolumns);
	}
	
}

